package it.truestoryfactory.tsq.services.msg;

import it.truestoryfactory.tsq.game.util.Quiz;
import it.truestoryfactory.tsq.game.util.TableMacIp;

public class SetupMessage extends TSMessage {
	public static final String DEVICE_ADDRESS_TAG = "device_address";
	public static final String IP_ADDRESS_TAG = "ip_address";
	public static final String TYPE = "setup";
	public static final String TABLE_MAC_IP_TAG = "table_mac_ip";
	public static final String SIZE_ATTRIBUTE = "size";
	public static final String QUIZ_TAG = "quiz";
	public static final String N_TOPICS_ATTRIBUTE = "n_topics";
	public static final String QST_4_TOPIC_ATTRIBUTE = "qst4topic";
	public static final String SEC_4_QST_ATTRIBUTE = "sec4qst";
	public static final String TOPIC_TAG = "topic";
	public static final String TOPIC_CODE_ATTRIBUTE = "topic_code";
	public static final String QUESTION_NUMBER_TAG = "question_number";
	
	private TableMacIp table;
	private Quiz quiz;
	
	@Override
	public String toXML() {
		
		return startMessageTag("setup")+getTableMacIpElem()+getQuizElem()+endMessageTag();
	}
	
	
	public void setTableMacIp(TableMacIp table) {
		this.table=table;	
	}
	
	public void setQuiz(Quiz quiz) {
		this.quiz=quiz;
	}
	
	public TableMacIp getTableMacIp() {
		return table;
	}
	
	public String getTableMacIpElem () {
		String elem=new String();
		elem += "<table_mac_ip size=\"";
		elem +=table.getDevicesNumber();
		elem +="\">";
		for (int i = 0; i < table.getDevicesNumber(); i++) {
			elem+="<ip_address>";
			elem+=table.getIpByIndex(i);
			elem+="</ip_address>";
			elem+="<device_address>";
			elem+=table.getMacByIndex(i);
			elem+="</device_address>";
		}
		elem+="</table_mac_ip>";
		return elem;
		
	}
	
	public String getQuizElem() {
		String[] topics=quiz.getTopics();
		Integer[][] quizTable= quiz.getQuizTable(); 
		String elem=new String();
		elem += "<quiz n_topics=\"";
		elem +=topics.length;
		elem += "\" qst4topic=\"";
		elem +=quiz.getQst4topic();
		elem += "\" sec4qst=\"";
		elem +=quiz.getSec4qst();
		elem +="\">";
		for (int i = 0; i < topics.length; i++) {
			elem+="<topic topic_code=\"";
			elem+=topics[i];
			elem +="\">";
			for (int j = 0; j < quiz.getQst4topic(); j++) {
				elem+="<question_number>";
				elem+=quizTable[i][j];
				elem +="</question_number>";
			}
			elem+="</topic>";
			
		}
		elem+="</quiz>";
		return elem;
		
	}
	
	public Quiz getQuiz() {
		return quiz;
	}
}
